﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OAProject.Domain.Entities;
using OAProject.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace OAProject.API.Controllers
{
    /// <summary>
    /// 请假申请模块API控制器
    /// </summary>
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize]     //全局授权过滤器
    public class ApplicationAPIController : ControllerBase
    {
        //依赖注入
        private IAttendanceRepository _attendance;  //请假申请
        private IGenreRepository _repository;  //请假类别
        private IAttendancesRepository _Attendances; //请假类别申请表

        public ApplicationAPIController(IAttendanceRepository attendance, IGenreRepository repository, IAttendancesRepository attendancesRepository)
        {
            _attendance = attendance;
            _repository = repository;
            _Attendances = attendancesRepository;
        }

        /// <summary>
        /// 请假申请显示+分页+查询
        /// </summary>
        /// <param name="auditor">审核人</param>
        /// <param name="seate">状态</param>
        /// <param name="genreId">类别</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetIndex(string auditor = "", int seate = 0, int genreId = 0, int page = 1, int limit = 10)
        {
            List<Attendances> list = _Attendances.ShowAtten();
            if (auditor != null)
            {
                list.Where(x => x.Auditor.Contains(auditor));
            }
            else if (seate >0)
            {
                list.Where(x => x.Seate.Equals(seate)&& x.Auditor.Contains(auditor));
            }
            else if (genreId >0)
            {
                list.Where(x => x.GenreId.Equals(genreId)&& x.Seate.Equals(seate) && x.Auditor.Contains(auditor));
            }
            list = list.Skip((page - 1) * limit).ToList();
            return Ok(new
            {
                data = list,
                code = 0,
                msg = "",
                count = list.Count
            });
        }

        /// <summary>
        /// 请假申请的添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromForm] Attendance obj)   //[FromForm]是接受页面传过来的数据
        {
            int a = _attendance.Insert(obj);
            return Ok(a);
        }
        /// <summary>
        /// 删除请假
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delete(int id)
        {
            int a = _attendance.Delete(id);
            return Ok(a);
        }
        /// <summary>
        /// 请假反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Fantian(int id)
        {
            var a = _attendance.GetById(id);
            return Ok(new
            {
                data = a,
                code = 0
            });
        }
        /// <summary>
        /// 请假修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Uep(Attendance m)
        {
            int a = _attendance.Update(m);
            return Ok(a);
        }
    }
}
